 package com.dfrz.problem.action.staffmanage;

import org.apache.struts2.dispatcher.SessionMap;

import com.dfrz.problem.model.Employee;
import com.dfrz.problem.model.User;
import com.dfrz.problem.service.EmployeeService;
import com.dfrz.problem.service.SysAdminService;
import com.dfrz.problem.utils.IConstant;
import com.dfrz.problem.utils.Result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyEmployeeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private EmployeeService employeeService;
	private SysAdminService sysAdminService;
	private Employee employee;
	private String password;
	private User user;
	private String employeeEmail;
	private String employeeName;
	private String birthday;
	private String contact;
	private int sex;
	
	/**
	 * 查看个人信息
	 * @return
	 */
	public String info(){
		return SUCCESS;
	}
	
	/**
	 * 修改个人信息
	 * @return
	 */
	public String update(){
		return SUCCESS;
	}
	
	public String modifyEmployeeConfirm(){
		Result<Employee> result = employeeService.getEmployee(employeeEmail);
		employee = result.getResult();
		return SUCCESS;
	}
	
	/**
	 * 修改个人信息校验
	 * @return
	 */
	public String checkModify(){
		employee = employeeService.getEmployee(employeeEmail).getResult();
		employee.setEmployeeName(employeeName);
		employee.setBirthday(birthday);
		employee.setContact(contact);
		employee.setSex(sex);
		Result<Employee> result = employeeService.updateEmployee(employee);
		if(IConstant.UPDATE_LOSE.equalsIgnoreCase(result.getCode())){
			addActionError(getText(IConstant.UPDATE_LOSE));
			return INPUT;
		}
		
		return SUCCESS;
	}
	
	/**
	 * 修改密码校验
	 * @return
	 */
	public String checkUpdatePwd(){
		if(user.getLoginType() == 0){
			// 验证管理员密码
			if(!sysAdminService.checkPwd(password,user.getEmail())){
				addActionError("原密码输入有误!");
				return INPUT;
			}
		}else{
			// 验证员工密码
			if(!employeeService.checkPwd(password,employee.getEmployeeEmail())){
				addActionError("原密码输入有误!");
				return INPUT;
			}
		}
		
		if(!employee.getAgainPwd().equals(user.getPassword())){
			addActionError("两次输入的密码不一样!");
			return INPUT;
		}
		if(user.getLoginType() == 0){
			// 修改管理员密码
			sysAdminService.updatePwd(user.getEmail(),user.getPassword());
		}else{
			// 修改员工密码
			employeeService.updatePwd(user.getEmail(),user.getPassword());			
		}
		
		return SUCCESS;
	}
	
	public Employee getEmployee() {
		return employee;
	}
	
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public User getUser() {
		SessionMap<String, Object> session = (SessionMap<String, Object>) ActionContext
				.getContext().getSession();
		user = (User) session.get("user");
		return user;
	}
	
	public void setSysAdminService(SysAdminService sysAdminService) {
		this.sysAdminService = sysAdminService;
	}
	
	public void setEmployeeEmail(String employeeEmail) {
		this.employeeEmail = employeeEmail;
	}
	
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	
	public void setContact(String contact) {
		this.contact = contact;
	}
	
	public void setSex(int sex) {
		this.sex = sex;
	}
}
